package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.db.conn;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


public class orders_Add extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public orders_Add() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	//添加订单信息
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;");
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "GET,POST");
		PrintWriter out = response.getWriter();

		conn cn = new conn();
	


		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");

		String ono = df.format(new Date()).toString();
        String rid=request.getParameter("rid");
        String sdate=request.getParameter("d1");
        String edate=request.getParameter("d2");
        String omoney=request.getParameter("omoney");
        String smoney=request.getParameter("m1");
        String ymoney=request.getParameter("m2");
        String mid=request.getParameter("mid");
        String mname=request.getParameter("mname");
        String tel=request.getParameter("tel");
        String cardno=request.getParameter("cardno");
		String id=request.getParameter("id");

		
        String sql="insert into orders(ono,rid,sdate,edate,omoney,smoney,ymoney,mid,mname,tel,cardno,otime,oflag) " +
				"values('"+ono+"',"+rid+",'"+sdate+"','"+edate+"','"+omoney+"',"+smoney+","+ymoney+","+mid+",'"+mname+"','"+tel+"','"+cardno+"',now(),'待支付')";



		cn.update(sql);

		cn.update("update receives set flag='已使用' where id="+id);

		//遍历
		String jsonString = request.getParameter("room");
		String f = "";

		JSONArray jsonArray1 = JSONArray.fromObject(jsonString);
		for (int i = 0; i < jsonArray1.size(); i++) {
			JSONObject jb = jsonArray1.getJSONObject(i);

			if(jb.getString("flag2").equals("1"))
			{
				cn.update("insert into ordersmore(ono, rno) values ('"+ono+"','"+jb.getString("rno")+"')");
			}
		}

		JSONArray jsonarray = new JSONArray();
		JSONObject jsonobj = new JSONObject();
		jsonobj.put("jieguo", 1);
		jsonarray.add(jsonobj);
		out = response.getWriter();
		out.println(jsonarray);


	
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}

